package com.fangqiang.sprint_boot_web1.validate.server;

import com.fangqiang.sprint_boot_web1.validate.data.Person;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

@Service
public class ValidateTest {

    /**
     * spring-boot 已经默认把这个类放入容器了（LocalValidatorFactoryBean）
     */
    @Autowired
    private Validator validator;

    @Autowired
    private ValidateTest2 validateTest2;

    @PostConstruct
    public void init(){
        Errors errors = validator.validateObject(new Person());
        errors.getAllErrors().stream().forEach(e-> System.out.println(e.getDefaultMessage()));
    }

    @PostConstruct
    public void init1(){
        try {
            validateTest2.a(new Person());
        }catch (Throwable e){
            // 触发了校验
            System.out.println();
        }
    }
}
